package com.spring.security.app.config;

import com.spring.security.app.authentication.SpringLogoutSuccessHandler;
import com.spring.security.core.properties.SecurityProperties;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * AppBean配置
 *
 * @author seabed_moon
 */
@Configuration
@AllArgsConstructor
public class AppSecurityBeanConfig extends WebSecurityConfigurerAdapter {


    private final SecurityProperties securityProperties;

    /**
     * 退出时的处理策略配置
     *
     * @return org.springframework.security.web.authentication.logout.LogoutSuccessHandler
     */
    @Bean
    @ConditionalOnMissingBean(LogoutSuccessHandler.class)
    public LogoutSuccessHandler logoutSuccessHandler(){
        return new SpringLogoutSuccessHandler(securityProperties.getBrowser().getSignOutUrl());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/static/**");
    }
}
